package com.alstudio.utils.android.notification;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;

import com.alstudio.R;
import com.alstudio.utils.android.AndroidVersionCheckUtils;

public class ALNotification {

	private int mId;
	private boolean isShowing;
	private Notification mNotification;
	private ALNotificationType mType;
	private Context mContext;
	private NotificationCompat.Builder mBuilder;
	private int mProgressMax;
	private int mProgress;
	private CharSequence mContentTitle;
	private CharSequence mContentText;

	public ALNotification(Context context, ALNotificationType type,
			CharSequence tickerText) {
		// TODO Auto-generated constructor stub
		mContext = context;
		mType = type;

		mBuilder = new NotificationCompat.Builder(mContext);
		setTicker(tickerText);
	}

	public int getId() {
		return mId;
	}

	public void setId(int id) {
		this.mId = id;
	}

	public void setShowing(boolean isShowing) {
		this.isShowing = isShowing;
	}

	public boolean isShowing() {
		return isShowing;
	}

	public void setType(ALNotificationType type) {
		this.mType = type;
	}

	public ALNotificationType getType() {
		return mType;
	}

	public Notification build() {
		mNotification = mBuilder.build();

		if (mType == ALNotificationType.AL_NOTIFICATION_TYPE_PROGRESS) {
			if (!AndroidVersionCheckUtils.hasJellyBean()) {

				mNotification.contentView = new RemoteViews(
						mContext.getPackageName(),
						R.layout.progress_notification_layout);
				mNotification.contentView.setProgressBar(R.id.progressBar1,
						mProgressMax, mProgress, false);
				mNotification.contentView.setTextViewText(R.id.content_text,
						mContentText);

				mNotification.contentView.setTextViewText(R.id.content_title,
						mContentTitle);
			}
		}

		return mNotification;
	}

	public void setWhen(long when) {
		mBuilder.setWhen(when);
	}

	/**
	 * Set the small icon to use in the notification layouts. Different classes
	 * of devices may return different sizes. See the UX guidelines for more
	 * information on how to design these icons.
	 * 
	 * @param icon
	 *            A resource ID in the application's package of the drawble to
	 *            use.
	 */
	public void setSmallIcon(int icon) {
		mBuilder.setSmallIcon(icon);
	}

	/**
	 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an
	 * additional level parameter for when the icon is a
	 * {@link android.graphics.drawable.LevelListDrawable LevelListDrawable}.
	 * 
	 * @param icon
	 *            A resource ID in the application's package of the drawble to
	 *            use.
	 * @param level
	 *            The level to use for the icon.
	 * 
	 * @see android.graphics.drawable.LevelListDrawable
	 */
	public void setSmallIcon(int icon, int level) {
		mBuilder.setSmallIcon(icon, level);
	}

	/**
	 * Set the title (first row) of the notification, in a standard
	 * notification.
	 */
	public void setContentTitle(CharSequence title) {
		mBuilder.setContentTitle(title);
		mContentTitle = title;
	}

	/**
	 * Set the text (second row) of the notification, in a standard
	 * notification.
	 */
	public void setContentText(CharSequence text) {
		mBuilder.setContentText(text);
		mContentText = text;
	}

	/**
	 * Set the large number at the right-hand side of the notification. This is
	 * equivalent to setContentInfo, although it might show the number in a
	 * different font size for readability.
	 */
	public void setNumber(int number) {
		mBuilder.setNumber(number);
	}

	/**
	 * Set the large text at the right-hand side of the notification.
	 */
	public void setContentInfo(CharSequence info) {
		mBuilder.setContentInfo(info);
	}

	/**
	 * Set the progress this notification represents, which may be represented
	 * as a {@link android.widget.ProgressBar}.
	 */
	public void setProgress(int max, int progress, boolean indeterminate) {
		mProgressMax = max;
		mProgress = progress;
		mBuilder.setProgress(max, progress, indeterminate);
	}

	/**
	 * Supply a custom RemoteViews to use instead of the standard one.
	 */
	public void setContent(RemoteViews views) {
		mBuilder.setContent(views);
	}

	/**
	 * Supply a BigContenViews that will be used in Jelly Bean to display the
	 * expanded notification Remember that it will be displayed alongside native
	 * notifications, so making this RemoteView very different from the original
	 * ones is a bad idea.
	 */
	public void setBigContentView(RemoteViews bigContentViews) {
	}

	/**
	 * Set the text that is displayed in the status bar when the notification
	 * first arrives.
	 */
	public void setTicker(CharSequence tickerText) {
		mBuilder.setTicker(tickerText);
	}

	/**
	 * Set the text that is displayed in the status bar when the notification
	 * first arrives, and also a RemoteViews object that may be displayed
	 * instead on some devices.
	 */
	public void setTicker(CharSequence tickerText, RemoteViews views) {
		mBuilder.setTicker(tickerText, views);
	}

	/**
	 * Set the large icon that is shown in the ticker and notification.
	 */
	public void setLargeIcon(Bitmap icon) {
		mBuilder.setLargeIcon(icon);
	}

	/**
	 * Set the sound to play. It will play on the default stream.
	 */
	public void setSound(Uri sound) {
		mBuilder.setSound(sound);
	}

	/**
	 * Set the sound to play. It will play on the stream you supply.
	 * 
	 * @see Notification#STREAM_DEFAULT
	 * @see android.media.AudioManager for the <code>STREAM_</code> constants.
	 */
	public void setSound(Uri sound, int streamType) {
		mBuilder.setSound(sound, streamType);
	}

	/**
	 * Set the vibration pattern to use.
	 * 
	 * @see android.os.Vibrator for a discussion of the <code>pattern</code>
	 *      parameter.
	 */
	public void setVibrate(long[] pattern) {
		mBuilder.setVibrate(pattern);
	}

	/**
	 * Set the argb value that you would like the LED on the device to blnk, as
	 * well as the rate. The rate is specified in terms of the number of
	 * milliseconds to be on and then the number of milliseconds to be off.
	 */
	public void setLights(int argb, int onMs, int offMs) {
		mBuilder.setLights(argb, onMs, offMs);
	}

	/**
	 * Set whether this is an ongoing notification.
	 * 
	 * <p>
	 * Ongoing notifications differ from regular notifications in the following
	 * ways:
	 * <ul>
	 * <li>Ongoing notifications are sorted above the regular notifications in
	 * the notification panel.</li>
	 * <li>Ongoing notifications do not have an 'X' close button, and are not
	 * affected by the "Clear all" button.
	 * </ul>
	 */
	public void setOngoing(boolean ongoing) {
		mBuilder.setOngoing(ongoing);
	}

	/**
	 * Set this flag if you would only like the sound, vibrate and ticker to be
	 * played if the notification is not already showing.
	 */
	public void setOnlyAlertOnce(boolean onlyAlertOnce) {
		mBuilder.setOnlyAlertOnce(onlyAlertOnce);
	}

	/**
	 * Setting this flag will make it so the notification is automatically
	 * canceled when the user clicks it in the panel. The PendingIntent set with
	 * {@link #setDeleteIntent} will be broadcast when the notification is
	 * canceled.
	 */
	public void setAutoCancel(boolean autoCancel) {
		mBuilder.setAutoCancel(autoCancel);
	}

	/**
	 * Set the third line of text in the platform notification template. Don't
	 * use if you're also using {@link #setProgress(int, int, boolean)}; they
	 * occupy the same location in the standard template.
	 */
	public void setSubText(CharSequence subtext) {
		mBuilder.setSubText(subtext);
	}

	/**
	 * Supply a {@link PendingIntent} to send when the notification is clicked.
	 * If you do not supply an intent, you can now add PendingIntents to
	 * individual views to be launched when clicked by calling
	 * {@link RemoteViews#setOnClickPendingIntent
	 * RemoteViews.setOnClickPendingIntent(int,PendingIntent)}. Be sure to read
	 * {@link Notification#contentIntent Notification.contentIntent} for how to
	 * correctly use this.
	 */
	public void setContentIntent(PendingIntent intent) {
		mBuilder.setContentIntent(intent);
	}

	public static PendingIntent backToApp(Context context) {

		Intent notificationIntent = new Intent(context, context.getClass());
		notificationIntent.setAction(Intent.ACTION_MAIN);
		notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
				notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		return contentIntent;
	}

	public enum ALNotificationType {
		// 普通通知
		AL_NOTIFICATION_TYPE_NORMAL,
		// 进度通知
		AL_NOTIFICATION_TYPE_PROGRESS,
	}

}
